package com.example.project22_editingmediaplayer;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.example.minimusicdemo.Main_play;
import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowArtis;
import com.example.project22_editingmediaplayer.objects.ArtisObject;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

public class LayoutArtists extends Fragment {
	Context myContext;
	ListView listview_artist;
	public static ArrayList<ArtisObject> listArtist;
	AudioUltility audioultility;
	public static ArrayList<SongObject> listArtistsong;
	RowArtis artistAdapter;

	public static Fragment newInstance(Context context) {
		LayoutArtists f = new LayoutArtists();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_artists,
				null);
		myContext = container.getContext();
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		listview_artist = (ListView) getActivity().findViewById(
				R.id.list_artist);
		listArtist = new ArrayList<ArtisObject>();
		audioultility = new AudioUltility(myContext);
		artistAdapter = new RowArtis(listArtist, myContext);
		listview_artist.setAdapter(artistAdapter);
		new LoadMusicByArtist().execute();
		listview_artist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				ArtisObject artisObject = artistAdapter.getItem(position);
				int id_artis = artisObject.get_id();
				listArtistsong = audioultility.getAllMusicByArtis(id_artis);

				Intent intent_artis = new Intent(getActivity(),
						ArtisListDetail.class);
				// intent_artis.putExtra("id_artis", id_artis);
				getActivity().startActivity(intent_artis);
			}
		});
		registerForContextMenu(listview_artist);
	}

	private class LoadMusicByArtist extends
			AsyncTask<Void, Void, ArrayList<ArtisObject>> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected ArrayList<ArtisObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return audioultility.getAllArtis();
		}
		@Override
		protected void onPostExecute(ArrayList<ArtisObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			artistAdapter.resetData(result);
		}

	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		// menu.setHeaderTitle("favorite");
		menu.add(0, v.getId(), 0, "Play All Artis");
		menu.add(0, v.getId(), 0, "Rename Playlist");
		menu.add(0, v.getId(), 0, "Delete Playlist");
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		if (item.getTitle() == "Play All Artis") {
			ArtisObject artisObject = artistAdapter.getItem(info.position);
			int id_artis = artisObject.get_id();
			listArtistsong = audioultility.getAllMusicByArtis(id_artis);
			MainActivity.mService.setlistsong(listArtistsong);
			SongResource.listSong = listArtistsong;
			artistAdapter.notifyDataSetChanged();
			MainActivity.mService.PlaySong(0);
			Intent intent = new Intent(getActivity(), Main_play.class);
			startActivity(intent);
		} else if (item.getTitle() == "Delete Playlist") {
			deleteplaylist();
		} else if (item.getTitle() == "Rename Playlist") {
			renameplaylist();
		} else {
			return false;
		}
		return true;

	}

	private void renameplaylist() {
		// TODO Auto-generated method stub

	}

	private void deleteplaylist() {
		// TODO Auto-generated method stub

	}

	private void playall() {
		// TODO Auto-generated method stub

	}
}